package com.example.gemfire.resources;

import com.example.gemfire.domain.BalanceTransfer;
import com.example.gemfire.repository.BalanceTransferRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/balanceTransfer")
public class BalanceTransferController {

    @Autowired
    private BalanceTransferRepository balanceTransferRepository;

    @GetMapping("")
    public HttpEntity<List<BalanceTransfer>> getAll() {
        List<BalanceTransfer> balanceTransferList = (List<BalanceTransfer>) balanceTransferRepository.findAll();
        if(balanceTransferList.size() == 0) {
            return new ResponseEntity<>((List<BalanceTransfer>) null, HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(balanceTransferList, HttpStatus.OK);
    }

    @GetMapping("/{key}")
    public HttpEntity<BalanceTransfer> getByKey(
            @PathVariable(value = "key") String key) {
        BalanceTransfer balanceTransfer = balanceTransferRepository.findByKey(key);
        return new ResponseEntity<>(balanceTransfer, balanceTransfer == null ? HttpStatus.NOT_FOUND: HttpStatus.OK);
    }

    @GetMapping("/value/{key}")
    public HttpEntity<Object> getValueByKey(
            @PathVariable(value = "key") String key) {
        BalanceTransfer balanceTransfer = balanceTransferRepository.findByKey(key);
        if(balanceTransfer == null || balanceTransfer.getValue() == null) {
            return new ResponseEntity<>("", HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(balanceTransfer.getValue(), HttpStatus.OK);
    }

    @PostMapping("")
    public HttpEntity<BalanceTransfer> add(
            @RequestBody BalanceTransfer balanceTransfer
    ) {
        if(balanceTransfer.getKey() == null) {
            return new ResponseEntity<>((BalanceTransfer) null, HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(balanceTransferRepository.save(balanceTransfer), HttpStatus.CREATED);
    }

    @PutMapping("")
    public HttpEntity<BalanceTransfer> update(
            @RequestBody BalanceTransfer balanceTransfer
    ) {
        if(balanceTransfer.getKey() == null) {
            return new ResponseEntity<>((BalanceTransfer) null, HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity<>(balanceTransferRepository.save(balanceTransfer), HttpStatus.OK);
    }

    @DeleteMapping("/{key}")
    public HttpEntity<Object> deleteByKey(
            @PathVariable(value = "key") String key) {
        balanceTransferRepository.deleteById(key);
        return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
    }

    @DeleteMapping("")
    public HttpEntity<Object> deleteAll() {
        balanceTransferRepository.deleteAll();
        return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
    }
}
